%% 模块对齐 缩短模块优化的时间，增加🦑时间

%LoadWorkModelsPath();
%InportTargetPos = 130;%输入端口可控距离接口
%OutportTargetPos = 130;%输出端口可控距离接口
InportTargetPos = app.Spinner_InportDistance.Value;
OutportTargetPos = app.Spinner_OutportDistance.Value;

MapModeleft = app.Button_OnlyLeft.Value;
MapModeRight = app.Button_OnlyRight.Value;
MapModeAll = app.NANButton.Value;

port_handles=get_param(gcbh,'PortHandles'); %gcbh获取当前模块端口句柄
Subsystem_Pos = get_param(gcbh,'Position');%获取当前模块位置信息

if (MapModeAll) || (MapModeleft)
    Inport_handles=port_handles.Inport; %获取当前模块输入端口句柄
    for InportNume=1:length(Inport_handles)
        line_handles=get_param(Inport_handles(InportNume),'Line');%获取信号线句柄
        linesrcmodel_handles=get_param(line_handles,'SrcBlockHandle');%获取信号线上的源模块句柄
        MidlineInport_pos=get(Inport_handles(InportNume),'Position');%获取输入端口输入线位置
    
        Inportsrcmodel_pos=get_param(linesrcmodel_handles,'Position'); %获取输入端口源模块位置
        Inportsrcmodel_len=Inportsrcmodel_pos(3)-Inportsrcmodel_pos(1);%获取输入端口源模块长度
        Inportsrcmodel_width=Inportsrcmodel_pos(4)-Inportsrcmodel_pos(2);%获取输入端口源模块宽度
    
        Inportnew_pos(1)=Subsystem_Pos(1)-InportTargetPos-Inportsrcmodel_len;%设置输入源端口x1位置距离Subsystem InportTargetPos+端口长度个单位
        Inportnew_pos(3)=Inportnew_pos(1)+Inportsrcmodel_len;%保证输入源端口模块长度不变
        Inportnew_pos(2)=MidlineInport_pos(2)-fix(Inportsrcmodel_width/2);%保证输入源端口y1位置处于平分线处
        Inportnew_pos(4)=MidlineInport_pos(2)+fix(Inportsrcmodel_width/2);%保证输入源端口宽度不变
    
        set_param(linesrcmodel_handles,'Position',Inportnew_pos);%设置信号线源模块新的位置
    end
end

if (MapModeAll) || (MapModeRight)
    outport_handles=port_handles.Outport;   %获取当前模块输出端口句柄      
    for OutportNum=1:length(outport_handles)
        line_handles = get_param(outport_handles(OutportNum),'Line');%获取信号线句柄
        linesrcmodel_handles =get_param(line_handles,'DstBlockHandle');%获取信号线上的终止模块句柄
        MidlineOutport_pos=get(outport_handles(OutportNum),'Position');%获取输出端口输出线位置
    
        Outportsrcmodel_pos=get_param(linesrcmodel_handles,'Position');%获取输出端口终止模块位置
        Outportsrcmodel_len=Outportsrcmodel_pos(3)-Outportsrcmodel_pos(1);%获取输出端口终止模块长度
        Outportsrcmodel_width=Outportsrcmodel_pos(4)-Outportsrcmodel_pos(2);%获取输出端口终止模块宽度
    
        Outportnew_pos(1)=Subsystem_Pos(3)+OutportTargetPos;%设置输出终止端口x1位置距离Subsystem OutportTargetPos个单位
        Outportnew_pos(3)=Outportnew_pos(1)+Outportsrcmodel_len;%保证输出终止端口模块长度不变
        Outportnew_pos(2)=MidlineOutport_pos(2)-fix(Outportsrcmodel_width/2);%保证输出终止端口y1位置处于平分线处
        Outportnew_pos(4)=MidlineOutport_pos(2)+fix(Outportsrcmodel_width/2);%保证输出终止端口宽度不变
    
        set_param(linesrcmodel_handles,'Position',Outportnew_pos);%设置信号线终止模块新的位置
    end
end
